<?php

import("data.db.file.DbFileInfo");

class DbGameInfo extends DbFileInfo {
	
	public $className = "data.logic.file.GameInfo";
	const CLASS_NAME = "data.logic.file.GameInfo";
    
	const TB_TABLENAME	= "pd_game_info";
	const TB_I			= 'gm';
	
	const TB_RL_NAME		= 'gm_real_name';
	const TB_RP_NAME		= 'gm_repr_name';
	const TB_SRV_NAME		= 'gm_serv_name';
	const TB_SIZE			= 'gm_size';
	const TB_VERSION		= 'gm_version';
	const TB_STATUS			= 'gm_status';
	const TB_DESCRIPTION	= 'gm_description';
	const TB_DIM			= 'gm_dim';
	const TB_CATEGORY		= 'gm_catalogue';
	const TB_TITLE			= 'gm_title';
	const TB_THUMB			= 'gm_thumb_name';
	
	public static function _get_TABLENAME() {return self::TB_TABLENAME;}
	public static function _get_ID() {return self::TB_I."_id";}
	public static function _get_SUFFIX() {return self::TB_I;}
	
	function forwardForeignCollection() {
		return parent::forwardForeignCollection();
	}
	function backwardForeignCollection() {
		return parent::forwardForeignCollection();
	}
	
	const THUMBNAIL_DEFAULT	= 'no_thumbnail.jpg';
	
	// properties
	public  $realName;
	public $repName;
	public $servName;
	public $size;
	public $version;
	public $status;
	public $description;
	public $categoryId;
	public $dim;
	public $title;
	public $thumbnailName;
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function setRealName($name) {$this->realName = $name;}
	function getRealName() {return $this->realName;}
	
	function setServName($name) {$this->servName = $name;}
	function getServName() {return $this->servName;}
	
	function setRepName($name) {$this->repName = $name;}
	function getRepName() {return $this->repName;}
	
	function setSize($val) {$this->size = $val;}
	function getSize() {return $this->size;}
	
	function setVersion($val) {$this->version = $val;}
	function getVersion() {return $this->version;}
	
	function setStatus($val) {$this->status = $val;}
	function getStatus() {return $this->status;}
	
	function getThumbnail() {
		$th_name = substr($this->repName, 0, -4)."_th.jpg";
		if (file_exists(REPOSITORY_PATH.DIRSEP.$th_name)) {
			return $th_name;
		} else {
			return null;
		}
	}
	
	function setDescription($val) {$this->description = $val;}
	function getDescription() {return $this->description;}
	
	function setCategoryId($val) {$this->categoryId = $val;}
	function getCategoryId() {return $this->categoryId;}
	
	function getCategoryName() {
		$lingvo = Bundle::findByKey($this->categoryId);
		if ($lingvo) return $lingvo->getValue();
		return "";
	}
	
	function setTitle($value) {$this->title = $value;}
	function getTitle() {return $this->title;}
	
	function setDim($val) {$this->dim = $val;}
	function getDim() {return $this->dim;}
	
	function setThumbnailName($value) {$this->thumbnailName = $value;}
	function getThumbnailName() {return $this->thumbnailName;}
	
	
	// restore
	function fillEntity($arrResult) {
		DbAbstractTable::fillEntity($arrResult);
		$this->setRealName($arrResult[self::TB_RL_NAME]);
		$this->setRepName($arrResult[self::TB_RP_NAME]);
		$this->setServName($arrResult[self::TB_SRV_NAME]);
		$this->setSize($arrResult[self::TB_SIZE]);
		$this->setVersion($arrResult[self::TB_VERSION]);
		$this->setStatus($arrResult[self::TB_STATUS]);
		$this->setDescription($arrResult[self::TB_DESCRIPTION]);
		$this->setDim($arrResult[self::TB_DIM]);
		$this->setCategoryId($arrResult[self::TB_CATEGORY]);
		$this->setTitle($arrResult[self::TB_TITLE]);
		$this->setThumbnailName($arrResult[self::TB_THUMB]);
	}
	
	//store
	function fillFields() {
		DbAbstractTable::fillFields();
		$this->fildsForStore[self::TB_RL_NAME] = "'".$this->getRealName()."'";
		$this->fildsForStore[self::TB_RP_NAME] = "'".$this->getRepName()."'";
		$this->fildsForStore[self::TB_SRV_NAME] = "'".$this->getServName()."'";
		$this->fildsForStore[self::TB_SIZE] = "'".$this->getSize()."'";
		$this->fildsForStore[self::TB_VERSION] = "'".$this->getVersion()."'";
		$this->fildsForStore[self::TB_STATUS] = "'".$this->getStatus()."'";
		$this->fildsForStore[self::TB_DESCRIPTION] = "'".$this->getDescription()."'";
		$this->fildsForStore[self::TB_DIM] = "'".$this->getDim()."'";
		$this->fildsForStore[self::TB_CATEGORY] = "'".$this->getCategoryId()."'";
		$this->fildsForStore[self::TB_TITLE] = "'".$this->getTitle()."'";
		$this->fildsForStore[self::TB_THUMB] = "'".$this->getThumbnailName()."'";
	}
	
	function findByRealName($name) {
		return parent::__selectByCriteria(
			self::TB_RL_NAME."='".$name."'", self::CLASS_NAME);
	}
	
	function findByRepoName($name) {
		return parent::__selectByCriteria(
			self::TB_RP_NAME."='".$name."'", self::CLASS_NAME);
	}
	
	function findAllByCategory($category) {
		//TODO: category name must be replaced by category id
		return parent::__selectAllByCriteria(
			self::TB_CATEGORY."='".$category."'", self::CLASS_NAME);
	}
	
	// same for all classes
    public static function findAll() {return DbAbstractTable::findAll(self::CLASS_NAME);}
    public static function findById($id) {return DbAbstractTable::findById($id, self::CLASS_NAME);}
}
?>